﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;

public enum PlayerIndex {
    P1,
    P2
}

//玩家控制类（人物和载具）
public class PlayerController : MonoBehaviour {

    public HeroProperty heroProperty;//玩家人物属性
    public Transform[] fireTrans;//开火或投掷点(1F,2F,3F|4FT)
    public Transform downRay;//判断是否在地面上
    public PlayerIndex playerIndex;//识别是第几个玩家（总共俩）
    [Header("向下射线的长度")]
    public float rayDistance;

    protected List<ControllerConfig> controlConfig;//键盘控制配置文件
    protected float Xaddition, Yaddition, jumptime;//X轴增量和Y轴增量
    protected uint curAmmo, curBomb;//当前子弹和炸弹数量
    protected bool isJump;//是否可以跳跃
    protected Rigidbody2D playerRigi;//玩家刚体
    protected Animator playerAnim;//玩家动画控制器

    /// <summary>
    /// 重置人物(武器)状态
    /// </summary>
    protected void OnReSet()
    {
        curAmmo = heroProperty.ammoNumber;
        curBomb = heroProperty.bombNumber;
    }/// <summary>
     /// 初始化玩家控制器，获得需要的组件
     /// </summary>
    protected void Init()
    {
        playerRigi = GetComponent<Rigidbody2D>();
        playerAnim = transform.GetComponentInChildren<Animator>();
        controlConfig = JsonMapper.ToObject<List<ControllerConfig>>(Resources.Load<TextAsset>("Configuration").text);
    }
    /// <summary>
    /// 模拟Update，在子类的Update中调用
    /// </summary>
    protected void OnFixedUpdate(){
        Xaddition = Input.GetAxisRaw(controlConfig[(int)playerIndex].horizontal);
        Yaddition = Input.GetAxisRaw(controlConfig[(int)playerIndex].vertical);

        playerRigi.velocity = new Vector2(Xaddition * heroProperty.moveSpeed,playerRigi.velocity.y);
        PlayerMove();
        if (Input.GetButton(controlConfig[(int)playerIndex].fire))
        {
            PlayerFire();
        }
        if (Input.GetButton(controlConfig[(int)playerIndex].bmob))
        {
            PlayerThrow();
        }
    }
    protected void Update() //为了实现平滑跳跃在FixedUpdate中调用
    {
        PlayerJump();
    }
    protected virtual void PlayerMove(){
        playerRigi.MovePosition(transform.position + new Vector3(Xaddition * heroProperty.moveSpeed, 0, 0) * Time.deltaTime);
    } //玩家移动方法
    protected virtual void PlayerJump(){
        if (Input.GetButtonDown(controlConfig[(int)playerIndex].jump) && IsGround())
        {
            isJump = true;
            jumptime = heroProperty.jumpTime;
            playerRigi.velocity = Vector2.up * heroProperty.jumpForce;
        }
        if (Input.GetButton(controlConfig[(int)playerIndex].jump) && isJump)
        {
            if(jumptime > 0){
                playerRigi.velocity = Vector2.up * (heroProperty.jumpForce / 2);
                jumptime -= Time.deltaTime;
            }
            else{
                isJump = false;
            }
        }
        if(Input.GetButtonUp(controlConfig[(int)playerIndex].jump)){
            isJump = false;
        }
    } //玩家跳跃方法
    protected virtual void PlayerFire(){
    } //玩家开火方法
    protected virtual void PlayerThrow(){
    } //玩家投掷炸弹方法

    protected bool IsGround()//判断是否在地面上
    {
        return Physics2D.Raycast(downRay.position, -downRay.up, rayDistance, layerMask: 1 << 8);
    }
}

//if判断里面
//isJump = true;
//StartCoroutine(JumpRoutine());
//调用的方法
//protected IEnumerator JumpRoutine() //控制人物跳跃的方法（在跳跃方法中调用）
//{
//    playerRigi.velocity = Vector2.zero;//重置加速度
//    playerRigi.gravityScale = 0;
//    jumptime = 0;
//    while (Input.GetButton(controlConfig[(int)playerIndex].jump) && jumptime < heroProperty.jumpTime)
//    {
//        float proportionCompleted = jumptime / heroProperty.jumpTime;
//        Vector2 thisFrameJumpVector = Vector2.Lerp(new Vector2(Xaddition * heroProperty.moveSpeed, heroProperty.jumpForce), Vector2.zero, proportionCompleted);//平滑过渡
//        playerRigi.AddForce(thisFrameJumpVector);//施加力
//        jumptime += Time.deltaTime;
//        yield return null;
//    }
//    isJump = false;
//    playerRigi.gravityScale = 20;//加强重力
//}